//<SCRIPT>
function Files(params){
	this.__fileID = "lblFile";
	this.__pageCount = "lblPageCount";
	this.__pageID = "divPage";
	this.__pagesID = "divPages";
	this.__fileContID = "contFile";
	this.__container = "container";
	this.__selectBGColor = "navy";
	this.__selectColor = "white";
	this.__typeNA = 0;
	this.__typePage = 1;
	this.__typeFile = 2;

	this.overFile = overFile;
	this.overPage = overPage;
	this.preSelect = preSelect;
	this.mouseDown = mouseDownFile;
//	this._selectFile = _selectFile;
	this.select = _select;
	this._isBrother = _isBrother;
	this._isCousin = _isCousin;
	this._isFile = _isFile;
	this._isPage = _isPage;
	this._type = _type;
	this._isSelected = _isSelected;
	this._oldFile = _oldFile;
	this._selectPage = _selectPage;
	this._fileCont = _fileCont;
	this.selectIt = _selectIt;
	this._pageCountCont = _pageCountCont;
	this._fileLabel = _fileLabel;
	this._fileName = _fileName;
	this._pagesCont = _pagesCont;
	this._pageCount = _pageCount;
	this._pageNum = _pageNum;
	this._container = _container;
	this._contID = _contID;
	this.getNode = getNode;
	this.getFileNode = getFileNode;
	this.getPageNode = getPageNode;
	this.initPageTag = initPageTag;
	this.appendPage = appendPage;
	this.addPage = addPage;
	this.showPages = showPages;
	this.showFiles = showFiles;
	this.deletePage = deletePage;
	this.deletePages = deletePages;
	this.deleteFile = deleteFile;

	var p;
	for( p in params )
		this[p] = params[p];
	if( !this.__histObj )alert("Files:\nNo history object reference was provided to constructor.")
	///
	//******* Event handlers *******
	function mouseDownFile(){
		this._select();
	}
	function overFile(out){
		var e = this.getFileNode();	if( e )e.style.textDecorationUnderline = !out;
		this.overPage(out);
	}
	function overPage(out){
		var e = this.getPageNode();	if( !e )return;	e.style.textDecorationUnderline = !out;
	}
	function preSelect(e){
		if( !defined(e) )e = event.srcElement;
		if( event.button == 2 )return;
		if( !this._isPage(e) && !this._isFile(e) )return;
//		var r = document.body.createTextRange(); r.moveToElementText(e); r.select();
		try{
			if( false && e )setTimeout("document.selection.empty();",1000);
			else{
				var r = document.body.createTextRange();
				r.moveToElementText(e); r.findText('~~'); r.select();
			}
//			setTimeout("status='TextRange.length:"+document.selection.TextRange.length+"'",1000)
		}catch(exp){/*setTimeout("status='"+exp.description.replace(/'/g,'"')+"'",1000)*/}
	}
	//******* Methods *******
	function showFiles(el,show){
		var contFiles = el.all[this.__fileContID];
		for( var i=0; contFiles && i < contFiles.length; i++ )
			this.showPages(contFiles[i],show);
	}
	function showPages(e,show){
		e = e || event.srcElement;
		if( !defined(show) )return this._pagesCont(e).style.display==""?true:false;
		this._pagesCont(e).style.display = !show?"none":"";
	}
	function getFileNode(){return this.getNode(this.__fileID);}
	function getPageNode(){return this.getNode(this.__pageID);}
	function getNode(id){
		var e = window.event.srcElement.id == id ? window.event.srcElement : null;
//		if( e ) window.event.cancelBubble = true;
		return e;
	}
	function _select(e,not){
		if( typeof(e) == "number" ){
			e = this.__histObj.Get(e);
			if( !e )return;
		}
		e = e || event.srcElement;
//		if( this._isFile(e) )return this._selectFile(e,not);
//		if( this._isPage(e) )return this._selectPage(e,not);
		return this.selectIt(e,not);
	}
	function _selectIt(e,not){
		e = e || event.srcElement;
		if( !this._isPage(e) && !this._isFile(e) )return null;
		if( not ){
			if( defined(e.b) ){
				e.style.backgroundColor = e.b;
				e.style.color = e.c;
			}
			this.__histObj.Pop(e);
		}else
			if( !this.__histObj.Get(e) ){
				if( !defined(e.b) ){
					e.b = e.style.backgroundColor;
					e.c = e.style.color;
				}
				e.style.backgroundColor = this.__selectBGColor;
				e.style.color = this.__selectColor;
				for( var he=null; (he=this.__histObj.Pop(0)) && arguments.callee != _selectIt.caller; )
					this.selectIt(he,true);
				this.__histObj.Put(e);
			}
//		if( this._isPage(e) && e.parentElement )this.selectIt(this._fileLabel(e),not);
		return e;
	}
	function ___selectFile(e,not){
		e = e || this.getFileNode();
		if( !e )return;
		if( e == -1 )e = _selectFile.__selectFile_old1;
		e = this._fileLabel(e);
		this.selectIt(e,not);
		var old = selectHist();
		if( old && old != e )this.selectIt(old,true);
		if( !not )selectHist(e);
		///Locals
	}
	function _selectPage(e,not){
		e = e || this.getPageNode();
		if( !e )return;
		this.selectIt(e,not);
		var old = selectHist();
		if( old && old != e )this._selectIt(old,not);
		if( !not )selectHist(e);
	}
	function initPageTag(e){
		e.id = this.__pageID;	return e;
	}
	function addPage(e){
	var page = this._pageNum(e,this._pageCount(e)-1);
	var clone = this.appendPage(e,page.cloneNode(true));
		return this._pageNum(e,this._pageCount(e)-1,this._pageCount(e));
	}
	function appendPage(e,pg){return this._pagesCont(e).appendChild(this.initPageTag(pg))}
	function deletePages(e,count){
		var pageCont=this._pagesCont(e);
		count = defined(count) ? count : this._pageCount(pageCont);
		for(  ;count-- > 0;	this.deletePage(pageCont) ){};
		return pageCont;
	}
	function deletePage(e){
		var pc = this._pageCount(e);
		if( pc == 0 )return null;
		var p = this._pageNum(e,pc-1);
		this.selectIt(this._fileLabel(p),true);
		return p.parentElement.removeChild(p);
	}
	function deleteFile(e){
	var file = this._fileCont(e);
		file.parentElement.removeChild(file);
	}
	//******* Properties *******
	function _isSelected(e){
		e = e || event.srcElement;
		if( this._isFile(e) )return e.style.backgroundColor?true:false;
		if( this._isPage(e) )return e.style.backgroundColor?true:false;
	}
	function _isFile(e){return (e||event.srcElement) && (e||event.srcElement).id == this.__fileID }
	function _isPage(e){return (e||event.srcElement) && (e||event.srcElement).id == this.__pageID }
	function _type(e){return this._isPage(e) ? 1 : this._isFile(e) ? 2 : this._typeNA}
	function _isCousin(e1,e2){ return this._container(e1) == this._container(e2) }
	function _isBrother(e1,e2){ return this._fileName(e1) == this._fileName(e2) }
	function _container(e){return getParentByAttr(e||event.srcElement,this.__container)}
	function _contID(e){return this._container(e).id}
	function _fileCont(e){return getParentByAttr(e||event.srcElement,"id",this.__fileContID)}
	function _pagesCont(e){return this._fileCont(e).all[this.__pagesID]}
	function _pageCountCont(e){return this._fileCont(e).all[this.__pageCount]}
	function _fileLabel(e){return this._fileCont(e).all[this.__fileID]}
	function _fileName(e){return this._fileLabel(e).innerText}
	function _pageCount(e,count){
		var pc = this._pagesCont(e).all[this.__pageID];
		pc = !pc?0:(pc.length||1);
		(this._pageCountCont(e)||{}).innerHTML = defined(count) ? count : pc;
		return pc;
	}
	function _pageNum(e,num,newNum){
		e = e || event.srcElement;
		if( defined(num) ){
			var pg,pgs = this._pagesCont(e).all[this.__pageID];
			
			pg = defined(pgs && pgs.length)?pgs[num]:(pgs||null);
			if( pg && defined(newNum) )pg.innerText = newNum;
//			if( num >= pgs.length )return alert("There is no page #"+num+" in this file.");
			return pg;
		}
		return isNaN(e.innerText)?alert(e.innerText+" is not a number."):parseInt(e.innerText)-1;
	}
	function _oldFile(e){
		if( defined(e) )_oldFile.__oldFile = e;
		return _oldFile.__oldFile;
	}
}
/////////////////////////////////////////////////////
function SelectHistory(){
	this.__OBJ_NAME = "SelectHistory";
	this.__NO_POS = Number.NEGATIVE_INFINITY;
	this.__INV_PARAM = ": Invalid parameter.";
	this.__hist = new Array();
	this.Put = Put;
	this.Get = Get;
	this.Pop = Pop;
	this.FindPos = FindPos;
	//Definitions
	function Put(e){
		this.__hist.unshift(e); 
	}
	function Get(e,rem){
		var ret;
		if( !defined(e) )e = 0;
		if( e==0 || e ){
			if( !isNaN(e) )ret = e == this.__NO_POS ? null : this.__hist[e<0?this.__hist.length+e:e]||null;
			if( typeof(e) == "object" )ret = this.Get(this.FindPos(e));
		}
		if( !defined(ret) )return alert( this._OBJ_NAME+"->"+"Get("+((e||{}).innerHTML||e)+")"+this.__INV_PARAM);
		if( rem && ret != null )return this.Pop(ret);
		return ret;
	}
	function Pop(e){
//		status = "Pop("+e+")";
		if( e==0 || e ){
			if( !isNaN(e) )return e == this.__NO_POS ? null : this.__hist.splice(e<0?this.__hist.length+e:e,1)[0];
			if( typeof(e) == "object" )return this.Pop(this.FindPos(e));
		}
		alert( this._OBJ_NAME+"->"+"Pop("+((e||{}).innerHTML||e)+")"+this.__INV_PARAM);
	}
	function FindPos(e){
		for( var i=0,a=this.__hist; i < a.length; i++ )
			if( a[i] == e )return i;
		return this.__NO_POS;
	}
}
